package com.stylefeng.guns.modular.utils;


import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.util.SpringContextHolder;

import java.io.IOException;

/**
 * @author lmshe
 * @create 2017-09-15 15:24
 * @desc 七牛上传文件工具类
 */
public class QiniuUtil {
    //开发key
    public static String ACCESS_KEY = SpringContextHolder.getBean(GunsProperties.class).getQiniuAccessKey();

    //开发密钥，周期更换
    public static String SECRET_KEY = SpringContextHolder.getBean(GunsProperties.class).getQiniuSecretKey();

    //请确保该存储空间已经存在
    public static String BUCKET_NAME = SpringContextHolder.getBean(GunsProperties.class).getQiniuBucketName();

    //七牛空间文件域名
    public static String IMG_DOMAIN = SpringContextHolder.getBean(GunsProperties.class).getQiniuImgDomain();

    //密钥配置
    public static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了,默认3600s
    public static String getUpToken() {
        return auth.uploadToken(BUCKET_NAME);
    }
    /**
     * @param FilePath 文件路径
     * @param key      上传到七牛后保存的文件名
     * @throws IOException
     */
    public static int upload(String FilePath, String key) {
        //创建上传对象
        UploadManager uploadManager = new UploadManager();
        try {
            //调用put方法上传
            Response res = uploadManager.put(FilePath, key, getUpToken());
            return res.statusCode;//返回200表示成功
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *
     * @param fileByte 文件字节数组
     * @param key 上传到七牛后保存的文件名
     * @return
     * @throws IOException
     */
    public static int uploadByte(byte[] fileByte, String key) {
        //创建上传对象
        UploadManager uploadManager = new UploadManager();
        try {
            //调用put方法上传
            Response res = uploadManager.put(fileByte, key, getUpToken());
            return res.statusCode;
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return 0;
    }
    /**
     * 获取下载地址
     *
     * @param key 上传到七牛后保存的文件名
     * @return
     */
    public static String getDownloadUrl(String key) {
        //调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间
        return auth.privateDownloadUrl(IMG_DOMAIN + key, 3600);
    }

    /**
     * 删除文件
     *
     * @param key 上传到七牛后保存的文件名
     * @throws QiniuException
     */
    public static void delete(String key) {
        //实例化一个BucketManager对象
        BucketManager bucketManager = new BucketManager(auth);
        try {
            //调用delete方法移动文件
            bucketManager.delete(BUCKET_NAME, key);
        } catch (QiniuException e) {
            e.printStackTrace();
            //捕获异常信息
            Response r = e.response;
        }
    }
    /**
     * @author lmshe
     * @create 11:03 2017/11/21
     * @desc 重命名文件
     */
    public static void rename(String key, String newKey) {
        BucketManager bucketManager = new BucketManager(auth);
        try {
            bucketManager.rename(BUCKET_NAME, key, newKey);
        } catch (QiniuException ex) {
            //如果遇到异常，说明移动失败
            ex.printStackTrace();
        }
    }
    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        //设置文件路径以及文件名
        //String file = "D:\\test\\test.docx";
        //String key = "test.docx";
        //上传文件
        //System.out.println(upload(file,key));
        //获取文件下载地址
        System.out.println(getDownloadUrl("3da06d3e-3bdb-4d5b-bc06-162607c085c0.jpg"));
    }
}
